package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.CitiesService;
import com.xingkeduo.business.operation.service.OfficeService;
import com.xingkeduo.business.operation.service.ShopsService;
import com.xingkeduo.business.operation.service.VendorService;
import com.xingkeduo.pojo.dto.Vendor.VendorDTO;
import com.xingkeduo.pojo.query.CitiesQuery;
import com.xingkeduo.pojo.query.OfficeQuery;
import com.xingkeduo.pojo.query.ShopsQuery;
import com.xingkeduo.pojo.query.VendorQuery;
import com.xingkeduo.pojo.table.CitiesTable;
import com.xingkeduo.pojo.table.VendorTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class VendorDomain {

    @Autowired
    private VendorService vendorService;

    @Autowired
    private CitiesService citiesService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private ShopsService shopsService;

    public VendorTable getVendorInfoById(long id) throws Throwable {

        VendorTable vendorTable = vendorService.selectById(id);

        return vendorTable;
    }

    //创建
    public int createVendor(VendorTable vendorTable) throws Throwable {

        if(!CommonUtil.isEmpty(vendorTable.getName())){
            List<VendorTable> vendorTables = vendorService.selectVendors(new VendorQuery().setNameRepeat(vendorTable.getName()));
            if(!CommonUtil.isEmpty(vendorTables)){
                throw new DataException(DataException.Type.DEFAULT, "大区名字已经存在");
            }
            vendorTable.setName(vendorTable.getName());
        }

        int createRes = vendorService.insert(vendorTable);
        if (createRes > 0){
            return createRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "大区创建失败");
        }
    }

    //修改
    public int modifyVendor(VendorTable vendorTable) throws Throwable {
        VendorTable vendorTable1 = vendorService.selectById(vendorTable.getId());
        //判断城市下分公司是否有状态为正常
        if(!CommonUtil.isEmpty(vendorTable.getStatus())){
            if(vendorTable.getStatus()==2){
                List<CitiesTable> citiesTableList= citiesService.selectLists(new CitiesQuery().setVendorId(vendorTable1.getId()).setStatus(1));
                if(!CommonUtil.isEmpty(citiesTableList) && citiesTableList.size() > 0){
                    throw new DataException(DataException.Type.DEFAULT, "该大区下，还存在“启用”状态的城市，无法停用大区!");
                }
            }
        }
        if(!CommonUtil.isEmpty(vendorTable.getName())){
            if(!vendorTable.getName().equals(vendorTable1.getName())){
                if(!CommonUtil.isEmpty(vendorTable.getName())){
                    List<VendorTable> vendorTables = vendorService.selectVendors(new VendorQuery().setNameRepeat(vendorTable.getName()));
                    if(!CommonUtil.isEmpty(vendorTables)){
                        throw new DataException(DataException.Type.DEFAULT, "大区的名字已经存在");
                    }
                    vendorTable.setName(vendorTable.getName());
                }
            }
        }


        int modifyRes = vendorService.update(vendorTable);
        if (modifyRes > 0){
            return modifyRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "大区修改失败");
        }
    }

    //删除
    public int delVendor(Long id) throws Throwable {


        //判断城市下分公司是否有状态为正常
        if(!CommonUtil.isEmpty(id)){
            List<CitiesTable> citiesTableList= citiesService.selectLists(new CitiesQuery().setVendorId(id).setStatus(1));
            if(!CommonUtil.isEmpty(citiesTableList) && citiesTableList.size() > 0){
                throw new DataException(DataException.Type.DEFAULT, "该大区下，还存在“启用”状态的城市，无法删除大区!");
            }
        }


        VendorTable vendorTable = new VendorTable();
        vendorTable.setId(id);
        vendorTable.setIsDelete(YesNoEnum.YES);
        int delRes = vendorService.delete(vendorTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

    //获取列表
    public List<VendorTable> getVendorlists(VendorQuery vendorQuery) throws Throwable {
        //查询已开启的
        List<VendorTable> vendorList = vendorService.selectVendors(vendorQuery);

        return vendorList;
    }

    //分页查询
    public Page<VendorDTO> getVendorPageList(VendorQuery vendorQuery) throws Throwable {
        Integer pageNo = vendorQuery.getPageNo();
        Integer pageSize = vendorQuery.getPageSize();
        Page<VendorTable> vendorTablePage = vendorService.selectPageLists(vendorQuery,pageNo,pageSize);

        List<VendorTable> dtoList = vendorTablePage.getDtoList();
        ArrayList<VendorDTO> objects = new ArrayList<>();
        if(!CommonUtil.isEmpty(dtoList)){
            for (VendorTable vendorTable : dtoList) {
                VendorDTO vendorDTO = new VendorDTO();
                BeanUtils.copyProperties(vendorTable,vendorDTO);
                int citiesNum = citiesService.count(new CitiesQuery().setVendorId(vendorTable.getId()).setStatus(1));
                vendorDTO.setCitiesNum(citiesNum);
                int officeNum = officeService.count(new OfficeQuery().setVendorId(vendorTable.getId()).setStatus(1));
                vendorDTO.setOfficeNum(officeNum);
                int shopsNum = shopsService.count(new ShopsQuery().setVendorId(vendorTable.getId()).setStatus(2));
                vendorDTO.setShopsNum(shopsNum);
                objects.add(vendorDTO);
            }
        }
        Page<VendorDTO> vendorDTOPage = new Page<>(pageNo,pageSize);
        BeanUtils.copyProperties(vendorTablePage, vendorDTOPage);
        vendorDTOPage.setDtoList(objects);
        return vendorDTOPage;
    }

}
